package com.android.gufengmall.cart;

import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.gufengmall.App;
import com.android.gufengmall.BaseActivity;
import com.android.gufengmall.R;
import com.android.gufengmall.adapter.CartAddressAdapter;
import com.android.gufengmall.adapter.CartOrdersAdapter;
import com.android.gufengmall.bean.AddressBean;
import com.android.gufengmall.bean.OrderBean;
import com.android.gufengmall.gen.AddressBeanDao;
import com.android.gufengmall.gen.DaoSession;
import com.android.gufengmall.gen.GoodsBeanDao;
import com.android.gufengmall.gen.OrderBeanDao;
import com.luck.picture.lib.tools.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;

public class CartDetailsActivity extends BaseActivity {

    private CartAddressAdapter mCartAddressAdapter;
    private CartOrdersAdapter mCartGoodsAdapter;

    private List<AddressBean> mAddressBeanList;

    private RecyclerView mRvAddress;
    private RecyclerView mRvGoods;
    private Button mBtnShop;

    private App mMyApp;
    private DaoSession mDaoSession;
    private List<OrderBean> mOrderBeans;
    private TextView mTvTime;
    private TextView mTvNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart_details);

        initView();
        initData();
        initListener();
    }

    private void initListener() {
        mBtnShop.setOnClickListener(v -> {
            ToastUtils.s(this,"购买成功");
        });
    }

    private void initData() {
        mMyApp = (App) this.getApplication();
        mDaoSession = mMyApp.getDaoSession();
        AddressBeanDao addressBeanDao = mDaoSession.getAddressBeanDao();
        OrderBeanDao orderBeanDao = mDaoSession.getOrderBeanDao();
        GoodsBeanDao goodsBeanDao = mDaoSession.getGoodsBeanDao();
        mOrderBeans = orderBeanDao.loadAll();
        mAddressBeanList = addressBeanDao.loadAll();

        mRvAddress.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mCartAddressAdapter = new CartAddressAdapter(this, mAddressBeanList);
        mRvAddress.setAdapter(mCartAddressAdapter);

        mRvGoods.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mCartGoodsAdapter = new CartOrdersAdapter(this, mOrderBeans);
        mRvGoods.setAdapter(mCartGoodsAdapter);

        //当前时间
        SimpleDateFormat sdfTwo =new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒E", Locale.getDefault());
        long timeGetTime =new Date().getTime();
        mTvTime.setText(sdfTwo.format(timeGetTime));

        //生成随机订单编号
        Random random = new Random();
        long nextLong = random.nextLong();
        String value = String.valueOf(nextLong);
        String substring = value.substring(1, 18);
        mTvNumber.setText(substring);
    }

    private void initView() {
        mRvAddress = findViewById(R.id.rv_address);
        mRvGoods = findViewById(R.id.rv_goods);
        mBtnShop = findViewById(R.id.btn_shop);
        mTvTime = findViewById(R.id.tv_time);
        mTvNumber = findViewById(R.id.tv_order_number);
    }
}